# -*- encoding: utf-8 -*-

# Copyright 2010 István Szentandrási
# <etude-music-player@googlegroups.com>

# This file is part of Etude.
#
# Etude is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as 
# published by the Free Software Foundation, either version 3 
# of the License, or (at your option) any later version.
#
# Etude is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Etude. If not, see <http://www.gnu.org/licenses/>.

import etude.plugins
etude.plugins.install_gettext(__package__, domain = 'defaults')
import etude.client as ec
import gtk
import etude.gui as eg
import etude.config as ecfg
_b = __builtins__['_']

class PropertyOrderFrame(gtk.Frame):

    def __init__(self, current_patt):
        gtk.Frame.__init__(self)
        self.set_shadow_type(gtk.SHADOW_NONE)
        self.curr_struct = ecfg.parse_column_xmms(current_patt)
        table = gtk.Table(rows = 4, columns = 2)

        
        self.combo = eg.get_property_combo(translate = True)
        
        pattern_store = gtk.ListStore( str, str)
        for value in self.curr_struct:
            pattern_store.append( (_b(value), value))
 
        self.pattern_list = gtk.TreeView(pattern_store)
        self.pattern_list.get_selection().set_mode( gtk.SELECTION_SINGLE )
        rendererText = gtk.CellRendererText()
        column = gtk.TreeViewColumn(None, rendererText, text = 0)
        self.pattern_list.append_column(column)
        self.pattern_list.set_headers_visible(False)
        scrollw = gtk.ScrolledWindow()
        scrollw.add(self.pattern_list)

        table.attach(self.combo, 0, 1, 0, 1, xoptions = gtk.FILL,
                yoptions = gtk.FILL)
        table.attach(scrollw, 0, 1, 1, 4)

        add_button = gtk.Button(stock = gtk.STOCK_ADD)
        up_button = gtk.Button(stock = gtk.STOCK_GO_UP)
        rem_button = gtk.Button(stock = gtk.STOCK_REMOVE)
        down_button = gtk.Button(stock = gtk.STOCK_GO_DOWN)

        table.attach(add_button, 1, 2, 0, 1, xoptions = gtk.FILL, yoptions = 0)
        table.attach(up_button, 1, 2, 1, 2, xoptions = gtk.FILL, yoptions = 0)
        table.attach(rem_button, 1, 2, 2, 3, xoptions = gtk.FILL, yoptions = 0)
        table.attach(down_button, 1, 2, 3, 4, xoptions = gtk.FILL, yoptions = 0)

        add_button.connect('clicked', self.add_clicked)
        up_button.connect('clicked', self.up_clicked)
        rem_button.connect('clicked', self.rem_clicked)
        down_button.connect('clicked', self.down_clicked)

        self.add(table)
        self.show_all()

    def add_clicked(self, widget, data = None):
        sel = self.combo.get_active_iter()
        if sel != None:
            model = self.combo.get_model()
            val1 = model.get_value(sel, 0)
            val2 = model.get_value(sel, 1)
            pattmodel = self.pattern_list.get_model()
            pattmodel.append( (val2, val1) )
        else:
            text = self.combo.get_active_text()
            if text:
                pattmodel = self.pattern_list.get_model()
                pattmodel.append( (_b(text), text))
        return False

    def up_clicked(self, widget, data = None):
        (model, sel_iter) = self.pattern_list.get_selection().get_selected()
        if sel_iter == None:
            return False
        path = model.get_path(sel_iter)[0]
        if path == 0:
            return False
        iter_prev = model.get_iter( (path-1,))
        model.swap(sel_iter, iter_prev)
        return False

    def down_clicked(self, widget, data = None):
        (model, sel_iter) = self.pattern_list.get_selection().get_selected()
        if sel_iter == None:
            return False
        iter_next = model.iter_next(sel_iter)
        if iter_next == None:
            return False
        model.swap(sel_iter, iter_next)
        return False

    def rem_clicked(self, widget, data = None):
        (model, sel_iter) = self.pattern_list.get_selection().get_selected()
        if sel_iter == None:
            return False
        model.remove(sel_iter)
        return False
    
    def get_selection(self):
        """
        returns a pattern
        """
        model = self.pattern_list.get_model()
        row_iter = model.get_iter_first()
        ret = ""
        while row_iter != None:
            ret += "|<"+model.get_value(row_iter, 1)+">"
            row_iter = model.iter_next(row_iter)
        if ret != "":
            ret = ret[1:]
        return ret

